<?php
 
 class node_Form_NodeSearch extends Zend_Form
 {
 	 protected $cName = 'node_Form_NodeSearch';
     protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );
    
    public function cluBuild($params)
    {
        $filterHtmlBody = new Clu_Filter_HtmlBody();
        $filterTrim = new Zend_Filter_StringTrim();
        $filterDigits = new Zend_Filter_Digits();
        $validatorNotEmpty = new Zend_Validate_NotEmpty();
        $myValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\.\-]*$/i'); //alpha, num, space, -, _
        $myValidator->setMessage(t(60), Zend_Validate_Regex::NOT_MATCH);

        $this->setMethod('GET');
        $actionUrl = $this->getView()->hvnUrl(array('module' => 'node', 
        											'controller' => 'master',
        											'type' => $params['type'],
        											'action' => $params['action']) , 'node');
    	$this->setAction($actionUrl);
    	
	    //terms 
    	$terms = new Zend_Form_Element_Text('terms', array('disableLoadDefaultDecorators' => true,
                                                                       'class' => 'inputText', 
                                                                       'required' => true));
        $terms->addDecorators($this->_noDecorator)
              ->addFilter($filterTrim);
        $this->addElement($terms);
        
        if($params['nodeTypeInfo']['module'] == 'node')
        	$moduleName = $params['nodeTypeInfo']['type'];
        else
        	$moduleName = $params['nodeTypeInfo']['module'];
        
        if(strpos($params['userPermissions'], "administer $moduleName"))
        {
		     //node category
		    if ($params['nodeTypeInfo']['has_category'] && !$params['searchAll'])
		    {
		    	//$params['nodeTypeInfo'] has 'category_table_name, 'root_category_id'
		    	//require_once (APP_PATH . '/modules/category/Model/Category.php');
		    	//require_once (APP_PATH . '/modules/category/Model/Relations.php');
		    	$mCategory = new category_Model_Category();
				$mCategory->setTables($params['nodeTypeInfo']['category_table_name']);
							// user-created category
				 
				if ($params['nodeTypeInfo']['category_creatable'])
				{
					//get $rootCategoryId;
					if ($params['nodeTypeInfo']['category_table_name'] != 'user')
						die("Something wrong with the node creation. User is now trying to create a category in ". $params['nodeTypeInfo']['category_table_name'] );
					$ret = $mCategory->getUserCategory($params['nodeTypeInfo']['type']);
					if (!$ret['success'])
						die($ret['errorMessage']);
					else
						$rootCategoryId = $ret['id'];
				}
				else
					$rootCategoryId = $params['nodeTypeInfo']['root_category_id'];
		
				$categories = $mCategory->getTree($rootCategoryId, 0, 2);
				
		        //$categories = $mCategory->getTree($params['nodeTypeInfo']['root_category_id'], 1, 2);
		 		$category = new Zend_Form_Element_Select('category',  
		 											  array('disableLoadDefaultDecorators' => true,
											          ));
														          
				//$category->addMultiOption('0','--'.t(113).'--');
		        foreach ($categories as $c) {
		            $category->addMultiOption($c['id'], p(trim($c['displayName'])));
		        	$category->addDecorators($this->_noDecorator);
		        }
		        $this->addElement($category);
		    }
		
		    //status
		    if (strpos ($params['userPermissions'], 'set status ' . $params['type']))
		    {
		        $status = new Zend_Form_Element_Select('status',array('disableLoadDefaultDecorators' => true,
		                                                                 'required' => true));
		        $status	->addMultiOption('-1', "-- ".t(61)." --")
		        		->addMultiOption('0',t(34))
		        	   ->addMultiOption('1',t(33))
		               ->addMultiOption('2',t(35))
		               ->addMultiOption('3',t(36))
		               ->addMultiOption('4',t(37))
		                ->setRegisterInArrayValidator(false)	
		                ->addDecorators($this->_noDecorator);
		        $this->addElement($status);
		    }
		
		    //items_perpage
		    $items_per_page = new Zend_Form_Element_Select('items_per_page', array('disableLoadDefaultDecorators' => true,
		                                                             'required' => true));
		    $items_per_page//->addMultiOption('0','--'.__('Choose').'--')
		                ->addMultiOption('10', 10)
		                ->addMultiOption('20', 20)
		                ->addMultiOption('50', 50)
		                ->addMultiOption('100', 100)
		                ->addDecorators($this->_noDecorator);
		    $this->addElement($items_per_page);
		
		    //poster_name 
			$poster_name = new Zend_Form_Element_Text('poster_name', array('disableLoadDefaultDecorators' => true,
		                                                                   'class' => 'inputText', 
		                                                                   'required' => true));
		    $poster_name->addDecorators($this->_noDecorator)
		                  ->addFilter($filterTrim);
		    $this->addElement($poster_name);
        }
              
        //order_by
        $order_by = new Zend_Form_Element_Select('order_by', array('disableLoadDefaultDecorators' => true,
                                                                 'required' => true));
		//order_by: created_time, or updated_time, views, ratings, number_of_ratings
        $order_by->addMultiOption('created', t(3008))
                    ->addMultiOption('updated', t(3009))
                    ->addMultiOption('views', t(3011))
                    ->addMultiOption('rating', t(117))
                    ->addMultiOption('number_of_ratings', t(3012))
                    ->addDecorators($this->_noDecorator);
        $this->addElement($order_by);

		$asc_or_desc = new Zend_Form_Element_Radio('asc_or_desc', 
													array('disableLoadDefaultDecorators' => true,
															'required' => true
	                                                                   ));
        $asc_or_desc->addMultiOption('DESC', t(123))
        			->addMultiOption('ASC', t(122))
        			->addDecorators($this->_noDecorator)
        			->setValue("DESC");
        			
        $this->addElement($asc_or_desc);
        
        
        // add the submit button
   		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                           'label' => "Search" ));
        
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);        
        
//        //node type, to be hidden
//		$type = new Zend_Form_Element_Hidden('type', array('disableLoadDefaultDecorators' => true,
//															'value' => $params['type']
//	                                                                   ));
//        $type->addDecorators($this->_noDecorator);
//		
//        $this->addElement($type);
        
    }
    /*
    public function populateCategory($data, $categoryId)
    {
    	//Populate rtype options 
    	if ($categoryId != 0)
		{
			$mCategory = new category_Model_Category();
			$mCategory->setTables('place');
		    $categories = $mCategory->getTree($categoryId, 1, 2);
		    function cmp($a, $b)
			{
			    return strcmp($a['name'], $b['name']);
			}
			usort($categories, "cmp");
		    
			foreach ($categories as $c) {
				$c = (array)$c;
				if($c['id'] != 0)
	          		$this->rtype->addMultiOption($c['id'], $c['displayName']);
     		}
		}
        //Populate district options 
    	if (isset($data['city_id']) && $data['city_id'] != 0)
	    {
	       	$mDistrict = new place_Model_District();
	    	$districts = $mDistrict->getDistricts($data['city_id']);
			foreach ($districts as $d) {
          	    $this->district_id->addMultiOption($d['district_id'], $d['district_name']);
     		}
        }
        //populate village options
    	if (isset($data['district_id']) && $data['district_id'] != 0)
	    {
			$mVillage = new place_Model_Village();
		    $villages = $mVillage->getVillages($data['district_id']);
			foreach ($villages as $v) {
          	    $this->village_id->addMultiOption($v['village_id'], $v['village_name']);
     		}
        }
        $data['category'] = $categoryId;
        $this->populate($data);
    }
*/
 }
 
?>
